package com.java.mybatis.controller;

import com.java.mybatis.entity.Goods;
import com.java.mybatis.entity.Move;
import com.java.mybatis.service.GoodsService;
import com.java.mybatis.service.MoveService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Move)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/move/")
public class MoveController {
    /**
     * 服务对象
     */
    @Resource
    private MoveService moveService;
    @Resource
    private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Move selectOne(Integer id) {
        return this.moveService.queryById(id);
    }

    /**
     * 分页查询移库记录表的数据
     * @param model
     * @return
     */
    @RequestMapping("queryMoveCordPage")
    public String queryMoveCordPage(Model model,Integer page)
    {
        try {
            List<Move> moves = moveService.queryMoveCordPage((page-1)*7,7);
            model.addAttribute("movesList",moves);
            model.addAttribute("thisPage",page);//当前页
            int countTotal = moveService.queryCountTotal();//查询总行数
            model.addAttribute("countTotal",countTotal);
            //总共有多少页
            int moveTotal = countTotal % 7 == 0 ? countTotal / 7 : countTotal / 7 + 1;
            model.addAttribute("pageSize1","当前第"+page+"页,共"+moveTotal+"页");
            //存入总共有多少页
            model.addAttribute("moveTotal",moveTotal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/member-level";
    }

    /**
     * 移库修改
     */
    @RequestMapping("updateGoodsMove")
    public String updateGoodsMove(Model model,Move move)
    {
        System.out.println("测试是否能获取到数据:"+move);
        int i = moveService.updateGoodsMove(move);//修改库存位置
        if(i>0){
            //将获取到的数据存入model中
            model.addAttribute("updateMove",i);
            //返回对应的页面
            return "System/member-level";
        }else{
            System.out.println("修改数据失败");
        }
        return "System/member-level";
    }

    /**
     * 移库修改
     */
    @RequestMapping("updateMove")
    public String updateMove(Model model,Move move,Integer mid,Integer page) throws Exception {
        System.out.println("测试是否能获取到数据:"+move);
        int i = moveService.updateMove(move,mid);//修改库存位置
        List<Goods> goodsList = goodsService.querySelPageInfo((page - 1) * 7, 7);
        if(i>0){
            //将获取到的数据存入model中
            model.addAttribute("updateMoves",i);
            model.addAttribute("goodsList",goodsList);
            //返回对应的页面
            return "System/member-view";
        }else{
            System.out.println("修改数据失败");
        }
        return "System/member-view";
    }
}